#!/bin/sh
module="scull"
device="scull"
mode="664"

if grep -q '^staff:' /etc/group; then
	group="staff"
else
	$group="wheel"
fi


# -e: file exist;
# -f: regular file exist.
# if [ ! -f /dev/${device}]	-->> means file not exist.
if [ -e /dev/${device} ]; then
	echo /dev/${device} "has exist, we remove it before.."
	./scull_unload
else
	echo "/dev/${device} not exist, we create it."
fi

# Invoke insmod with all arguments we got
# and use a pathname, as insmod doesn't look in. by default.
insmod ./$module.ko $* || exit 1
echo "insmod $module.ko complete."

# retrieve major number
major=$(awk "\$2==\"$module\" {print \$1}" /proc/devices)

echo "Create node in /dev. "
rm -f /dev/${device}[0-3]
mknod /dev/${device}0 c $major 0
mknod /dev/${device}1 c $major 1
mknod /dev/${device}2 c $major 2
mknod /dev/${device}3 c $major 3
ln -sf ${device}0 /dev/${device}
chgrp $group /dev/${device}[0-3]
chmod $mode  /dev/${device}[0-3]

echo "Create scullpipe node. "
rm -f /dev/${device}pipe[0-3]
mknod /dev/${device}pipe0 c $major 4
mknod /dev/${device}pipe1 c $major 5
mknod /dev/${device}pipe2 c $major 6
mknod /dev/${device}pipe3 c $major 7
ln -sf ${device}pipe0 /dev/${device}pipe
chgrp $group /dev/${device}pipe[0-3]
chmod $mode  /dev/${device}pipe[0-3]

echo "Load driver complete."
